package com.oinux.playhelper.ui;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.oinux.playhelper.R;
import com.oinux.playhelper.app.AppContext;
import com.oinux.playhelper.app.BaseActivity;
import com.oinux.playhelper.service.MainService;
import com.oinux.playhelper.widget.YesOrNoDialog;
import com.umeng.analytics.MobclickAgent;

public class Settings extends BaseActivity implements OnClickListener,
		OnTouchListener {

	private static final String TAG = "settings_activity";

	private TextView titleText;
	private RelativeLayout themeBtn;
	private RelativeLayout aboutBtn;
	private RelativeLayout crashBtn;
	private LinearLayout themeList;
	private LinearLayout activeTheme;
	private TextView activeThemeText;
	private LinearLayout simpleTheme;
	private TextView simpleThemeText;
	private TextView themeText;
	private ImageView themeIcon;
	private TextView aboutText;
	private ImageView aboutIcon;
	private TextView crashText;
	private ImageView crashIcon;
	private Button exitBtn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settings);
		initView();
		initData();
	}

	private void initData() {
		appContext = (AppContext) getApplication();
		int themeId = getThemeId();
		if (themeId == R.style.qinxin) {
			activeTheme.setBackgroundColor(getResources().getColor(
					R.color.setting_theme_item_selected));
			activeThemeText.setTextColor(getResources().getColor(
					R.color.setting_theme_item_text_selected));
			simpleTheme.setBackgroundColor(getResources().getColor(
					R.color.setting_theme_item));
			simpleThemeText.setTextColor(getResources().getColor(
					R.color.setting_theme_item_text));
		} else if (themeId == R.style.wenzhong) {
			simpleTheme.setBackgroundColor(getResources().getColor(
					R.color.setting_theme_item_selected));
			simpleThemeText.setTextColor(getResources().getColor(
					R.color.setting_theme_item_text_selected));
			activeTheme.setBackgroundColor(getResources().getColor(
					R.color.setting_theme_item));
			activeThemeText.setTextColor(getResources().getColor(
					R.color.setting_theme_item_text));
		}
	}

	private void initView() {
		titleText = (TextView) findViewById(R.id.title_bar_text);
		titleText.setText(R.string.settings);
		themeList = (LinearLayout) findViewById(R.id.theme_list);
		activeTheme = (LinearLayout) findViewById(R.id.active_theme);
		activeTheme.setOnClickListener(this);
		activeThemeText = (TextView) findViewById(R.id.active_theme_text);
		simpleTheme = (LinearLayout) findViewById(R.id.simple_theme);
		simpleTheme.setOnClickListener(this);
		simpleThemeText = (TextView) findViewById(R.id.simple_theme_text);
		themeBtn = (RelativeLayout) findViewById(R.id.theme);
		themeBtn.setOnTouchListener(this);
		themeBtn.setOnClickListener(this);
		aboutBtn = (RelativeLayout) findViewById(R.id.about);
		aboutBtn.setOnTouchListener(this);
		aboutBtn.setOnClickListener(this);
		crashBtn = (RelativeLayout) findViewById(R.id.crash);
		crashBtn.setOnTouchListener(this);
		crashBtn.setOnClickListener(this);
		themeIcon = (ImageView) findViewById(R.id.theme_icon);
		themeText = (TextView) findViewById(R.id.theme_text);
		aboutIcon = (ImageView) findViewById(R.id.about_icon);
		aboutText = (TextView) findViewById(R.id.about_text);
		crashText = (TextView) findViewById(R.id.crash_text);
		crashIcon = (ImageView) findViewById(R.id.crash_icon);
		exitBtn = (Button) findViewById(R.id.exit_helper_btn);
		exitBtn.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		if (v == themeBtn) {
			if (themeList.getVisibility() != View.VISIBLE) {
				themeList.setVisibility(View.VISIBLE);
			} else {
				themeList.setVisibility(View.GONE);
			}
		} else if (v == activeTheme) {
			themeList.setVisibility(View.GONE);
			switchTheme(R.style.qinxin);
		} else if (v == simpleTheme) {
			themeList.setVisibility(View.GONE);
			switchTheme(R.style.wenzhong);
		} else if (v == exitBtn) {
			YesOrNoDialog.Builder builder = new YesOrNoDialog.Builder(this);
			builder.setMessage(R.string.exit_tip).setTitle(R.string.exit);
			builder.setNegativeButton(R.string.no,
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
						}
					});
			builder.setPositiveButton(R.string.yes,
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
							appContext.count();
							stopService(new Intent(Settings.this,
									MainService.class));
							appContext.onTerminate();
						}
					});
			builder.create().show();
		}  else if (v == crashBtn) {
			YesOrNoDialog.Builder builder = new YesOrNoDialog.Builder(this);
			builder.setMessage(R.string.crash_tip).setTitle(R.string.crash);
			builder.setNegativeButton(R.string.no,
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
						}
					});
			builder.setPositiveButton(R.string.yes,
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
							appContext.crash();
						}
					});
			builder.create().show();
		} else if (v == aboutBtn) {
			startActivity(new Intent(this, AppAbout.class));
		}
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		if (v == themeBtn) {
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				themeBtn.setBackgroundColor(getResources().getColor(
						R.color.setting_item_pressed));
				themeIcon.setImageResource(R.drawable.theme_pressed);
				themeText.setTextColor(getResources().getColor(
						R.color.setting_item_text_pressed));
				return false;
			} else if (event.getAction() == MotionEvent.ACTION_UP) {
				themeBtn.setBackgroundColor(getResources().getColor(
						R.color.setting_item_normal));
				themeIcon.setImageResource(R.drawable.theme_normal);
				themeText.setTextColor(getResources().getColor(
						R.color.setting_item_text_normal));
				return false;
			}
		} else if (v == aboutBtn) {
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				aboutBtn.setBackgroundColor(getResources().getColor(
						R.color.setting_item_pressed));
				aboutIcon.setImageResource(R.drawable.about_pressed);
				aboutText.setTextColor(getResources().getColor(
						R.color.setting_item_text_pressed));
				return false;
			} else if (event.getAction() == MotionEvent.ACTION_UP) {
				aboutBtn.setBackgroundColor(getResources().getColor(
						R.color.setting_item_normal));
				aboutIcon.setImageResource(R.drawable.about_normal);
				aboutText.setTextColor(getResources().getColor(
						R.color.setting_item_text_normal));
				return false;
			}
		} else if (v == crashBtn) {
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				crashBtn.setBackgroundColor(getResources().getColor(
						R.color.setting_item_pressed));
				crashIcon.setImageResource(R.drawable.crash_pressed);
				crashText.setTextColor(getResources().getColor(
						R.color.setting_item_text_pressed));
				return false;
			} else if (event.getAction() == MotionEvent.ACTION_UP) {
				crashBtn.setBackgroundColor(getResources().getColor(
						R.color.setting_item_normal));
				crashIcon.setImageResource(R.drawable.crash_normal);
				crashText.setTextColor(getResources().getColor(
						R.color.setting_item_text_normal));
				return false;
			}
		}
		return false;
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}
}
